﻿using Common;
using DLL;
using gregn6Lib;
using NursingFormSystem;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using NursingFormSystem.NurFormsService;
using SQLDLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ShiftProject.Transfusion
{
    public partial class RecordsSafePrints : Form
    {
        private GridppReport Report = new GridppReport();
        NursingForm formService = new NursingForm();
        PatientInfo PatientInfo;
        string Id = string.Empty;
        Tb_User LoginUser;
        ElectronicSignature es;

        /// <summary>
        /// 多张打印 支持单张
        /// </summary>
        /// <param name="patientInfo">患者信息</param>
        /// <param name="id">若需要单张打印 请指定id 否则string.Empty </param>
        public RecordsSafePrints(Tb_User loginUser, PatientInfo patientInfo, string id)
        {
            InitializeComponent();
            PatientInfo = patientInfo;
            LoginUser = loginUser;
            this.Id = id;
            es = new ElectronicSignature(this.LoginUser);
            Report.LoadFromFile(System.Windows.Forms.Application.StartupPath + @"\GRFModel\Ydhl_GRF\安全输血护理.grf");
            Report.ConnectionString = Utility.GetDatabaseConnectionString();
            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
            axGRPrintViewer1.Report = Report;
        }
        private void ReportFetchRecord()
        {
            var ret = formService.GetNursingFormRecord(PatientInfo.PATIENT_ID, PatientInfo.VISIT_ID, (int)FormType.Transfuse);
            DataTable dt = new DataTable();
            if (ret.Code == 1)
            {
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                if (!string.IsNullOrEmpty(this.Id))
                    list = list.Where(p => p.Id.Equals(Convert.ToInt32(this.Id))).ToList();
                CreateDataTableHead(dt, list);
                for (int i = 0; i < list.Count; i++)
                {
                    NursingFormRecord record1 = list[i];
                    Transfuse fd = XmlUtil.XMLToObject<Transfuse>(record1.NursingFormData, Encoding.UTF8);
                    if (!string.IsNullOrEmpty(fd.ShuXueRiQi))
                        fd.ShuXueRiQi = Convert.ToDateTime(fd.ShuXueRiQi).ToString("yyyy-MM-dd");
                    string image1 = "";
                    string SXCHZ = fd.HeDuiNeiRong.SXQHDQM_ChuHeZhe;
                    if (!string.IsNullOrEmpty(SXCHZ))
                    {
                        image1 = es.GetElectricSignBase64(record1.AreaId, fd.HeDuiNeiRong.SXQHDQM_ChuHeZhe);
                    }
                    string image2 = "";
                    string SXFHZ = fd.HeDuiNeiRong.SXQHDQM_FuHeZhe;
                    if (!string.IsNullOrEmpty(SXFHZ))
                    {
                        image2 = es.GetElectricSignBase64(record1.AreaId, fd.HeDuiNeiRong.SXQHDQM_FuHeZhe);
                    }
                    string image3 = "";
                    string CPCHZ = fd.HeDuiNeiRong.CPHDQM_ChuHeZhe;
                    if (!string.IsNullOrEmpty(CPCHZ))
                    {
                        image3 = es.GetElectricSignBase64(record1.AreaId, fd.HeDuiNeiRong.CPHDQM_ChuHeZhe);
                    }
                    string image4 = "";
                    string CPFHZ = fd.HeDuiNeiRong.CPHDQM_FuHeZhe;
                    if (!string.IsNullOrEmpty(CPFHZ))
                    {
                        image4 = es.GetElectricSignBase64(record1.AreaId, fd.HeDuiNeiRong.CPHDQM_FuHeZhe);
                    }
                    dt.Rows.Add(
                        PatientInfo.PATIENT_NAME,//患者姓名
                        PatientInfo.SEX,//sex
                        PatientInfo.AGE,//age
                        PatientInfo.NURSE_CELL_CODE,//病区号
                        PatientInfo.DEPARTMENT_NAME,//科室名字
                        PatientInfo.BED_NO,//床号
                        PatientInfo.PATIENT_ID,//患者id
                        PatientInfo.NURSE_CELL_NAME,//病区名字

                        fd.XueXing.Equals("0") ? 1 : 0,
                        fd.XueXing.Equals("1") ? 1 : 0,
                        fd.XueXing.Equals("2") ? 1 : 0,
                        fd.XueXing.Equals("3") ? 1 : 0,

                        fd.RH.Equals("0") ? 1 : 0,
                        fd.RH.Equals("1") ? 1 : 0,

                        fd.ShuXueRiQi.Split('-')[0],
                        fd.ShuXueRiQi.Split('-')[1],
                        fd.ShuXueRiQi.Split('-')[2],

                        fd.HeDuiNeiRong.BingRenJiBenXinXi.Equals("0") ? 1 : 0,
                        fd.HeDuiNeiRong.BingRenJiBenXinXi.Equals("1") ? 1 : 0,

                        fd.HeDuiNeiRong.XueXing.Equals("0") ? 1 : 0,
                        fd.HeDuiNeiRong.XueXing.Equals("1") ? 1 : 0,
                        fd.HeDuiNeiRong.XueXing.Equals("2") ? 1 : 0,
                        fd.HeDuiNeiRong.XueXing.Equals("3") ? 1 : 0,

                        fd.HeDuiNeiRong.RH.Equals("0") ? 1 : 0,
                        fd.HeDuiNeiRong.RH.Equals("1") ? 1 : 0,

                        fd.HeDuiNeiRong.XueYeZhongLei.Split('#')[0].Equals("1") ? 1 : 0,
                        fd.HeDuiNeiRong.XueYeZhongLei.Split('#')[1].Equals("1") ? 1 : 0,
                        fd.HeDuiNeiRong.XueYeZhongLei.Split('#')[2].Equals("1") ? 1 : 0,
                        fd.HeDuiNeiRong.XueYeZhongLei.Split('#')[3].Equals("1") ? 1 : 0,
                        fd.HeDuiNeiRong.XueYeZhongLei.Split('#')[4].Equals("1") ? 1 : 0,
                        fd.HeDuiNeiRong.XueYeZhongLei.Split('#')[5].Equals("1") ? 1 : 0,
                        fd.HeDuiNeiRong.XueYeZhongLei.Split('#')[6].Equals("1") ? 1 : 0,
                        fd.HeDuiNeiRong.XueYeZhongLei.Split('#')[7].Equals("1") ? 1 : 0,

                        fd.HeDuiNeiRong.XYZL_QiTa = fd.HeDuiNeiRong.XueYeZhongLei.Split('#')[7].Equals("1") ? fd.HeDuiNeiRong.XYZL_QiTa : string.Empty,
                        fd.HeDuiNeiRong.XueYeShuLiang,
                        fd.HeDuiNeiRong.XueYeShuLiangml,

                        fd.HeDuiNeiRong.NingJiFanYing.Equals("0") ? 1 : 0,
                        fd.HeDuiNeiRong.NingJiFanYing.Equals("1") ? 1 : 0,
                        fd.HeDuiNeiRong.XueDaiWaiGuan.Equals("0") ? 1 : 0,
                        fd.HeDuiNeiRong.XueDaiWaiGuan.Equals("1") ? 1 : 0,

                        fd.HeDuiNeiRong.XueDaiTiaoXingMa.Equals("0") ? 1 : 0,
                        fd.HeDuiNeiRong.XueDaiTiaoXingMa.Equals("1") ? 1 : 0,

                        !Base.ESSwitch.Equals("on") ? fd.HeDuiNeiRong.SXQHDQM_ChuHeZhe : "",
                        !Base.ESSwitch.Equals("on") ? fd.HeDuiNeiRong.SXQHDQM_FuHeZhe : "",
                        !Base.ESSwitch.Equals("on") ? fd.HeDuiNeiRong.CPHDQM_ChuHeZhe : "",
                        !Base.ESSwitch.Equals("on") ? fd.HeDuiNeiRong.CPHDQM_FuHeZhe : "",

                        DateTimeHelper.HHmmssToHHmm(fd.JianCeNeiRongList[0].ShiJian),
                        DateTimeHelper.HHmmssToHHmm(fd.JianCeNeiRongList[1].ShiJian),
                        DateTimeHelper.HHmmssToHHmm(fd.JianCeNeiRongList[2].ShiJian),
                        DateTimeHelper.HHmmssToHHmm(fd.JianCeNeiRongList[3].ShiJian),
                        DateTimeHelper.HHmmssToHHmm(fd.JianCeNeiRongList[4].ShiJian),
                        DateTimeHelper.HHmmssToHHmm(fd.JianCeNeiRongList[5].ShiJian),

                        fd.JianCeNeiRongList[0].ShenZhi,
                        fd.JianCeNeiRongList[1].ShenZhi,
                        fd.JianCeNeiRongList[2].ShenZhi,
                        fd.JianCeNeiRongList[3].ShenZhi,
                        fd.JianCeNeiRongList[4].ShenZhi,
                        fd.JianCeNeiRongList[5].ShenZhi,

                        fd.JianCeNeiRongList[0].TiWen,
                        fd.JianCeNeiRongList[1].TiWen,
                        fd.JianCeNeiRongList[2].TiWen,
                        fd.JianCeNeiRongList[3].TiWen,
                        fd.JianCeNeiRongList[4].TiWen,
                        fd.JianCeNeiRongList[5].TiWen,

                        fd.JianCeNeiRongList[0].MaiBo,
                        fd.JianCeNeiRongList[1].MaiBo,
                        fd.JianCeNeiRongList[2].MaiBo,
                        fd.JianCeNeiRongList[3].MaiBo,
                        fd.JianCeNeiRongList[4].MaiBo,
                        fd.JianCeNeiRongList[5].MaiBo,

                        fd.JianCeNeiRongList[0].HuXi,
                        fd.JianCeNeiRongList[1].HuXi,
                        fd.JianCeNeiRongList[2].HuXi,
                        fd.JianCeNeiRongList[3].HuXi,
                        fd.JianCeNeiRongList[4].HuXi,
                        fd.JianCeNeiRongList[5].HuXi,

                        fd.JianCeNeiRongList[0].XueYa,
                        fd.JianCeNeiRongList[1].XueYa,
                        fd.JianCeNeiRongList[2].XueYa,
                        fd.JianCeNeiRongList[3].XueYa,
                        fd.JianCeNeiRongList[4].XueYa,
                        fd.JianCeNeiRongList[5].XueYa,

                        fd.JianCeNeiRongList[0].SuDu,
                        fd.JianCeNeiRongList[1].SuDu,
                        fd.JianCeNeiRongList[2].SuDu,
                        fd.JianCeNeiRongList[3].SuDu,
                        fd.JianCeNeiRongList[4].SuDu,
                        fd.JianCeNeiRongList[5].SuDu,

                        fd.JianCeNeiRongList[0].BuLiangFanYing,
                        fd.JianCeNeiRongList[1].BuLiangFanYing,


                        fd.JianCeNeiRongList[2].BuLiangFanYing.Equals("1") ? 1 : 0,
                        fd.JianCeNeiRongList[2].BuLiangFanYing.Equals("0") ? 1 : 0,

                        fd.JianCeNeiRongList[3].BuLiangFanYing.Equals("1") ? 1 : 0,
                        fd.JianCeNeiRongList[3].BuLiangFanYing.Equals("0") ? 1 : 0,

                        fd.JianCeNeiRongList[4].BuLiangFanYing.Equals("1") ? 1 : 0,
                         fd.JianCeNeiRongList[4].BuLiangFanYing.Equals("0") ? 1 : 0,

                        fd.JianCeNeiRongList[5].BuLiangFanYing.Equals("1") ? 1 : 0,
                         fd.JianCeNeiRongList[5].BuLiangFanYing.Equals("0") ? 1 : 0,

                        fd.JianCeNeiRongList[0].HuShiQianMing,
                        fd.JianCeNeiRongList[1].HuShiQianMing,
                        fd.JianCeNeiRongList[2].HuShiQianMing,
                        fd.JianCeNeiRongList[3].HuShiQianMing,
                        fd.JianCeNeiRongList[4].HuShiQianMing,
                        fd.JianCeNeiRongList[5].HuShiQianMing,

                        fd.BuLiangFanYingQingKuang.BLFYQK_NeiRong,
                        fd.BuLiangFanYingQingKuang.XueDaiBaoCun.Equals("0") ? 1 : 0,
                        fd.BuLiangFanYingQingKuang.XueDaiBaoCun.Equals("1") ? 1 : 0
                        );
                    //判断列是否存在并赋值
                    if (image1 != "" && dt.Columns.Contains("ElectricSignBase641"))
                    {
                        dt.Rows[0]["ElectricSignBase641"] = image1;
                    }
                    if (image2 != "" && dt.Columns.Contains("ElectricSignBase642"))
                    {
                        dt.Rows[0]["ElectricSignBase642"] = image2;
                    }

                    if (image3 != "" && dt.Columns.Contains("ElectricSignBase643"))
                    {
                        dt.Rows[0]["ElectricSignBase643"] = image3;
                    }

                    if (image4 != "" && dt.Columns.Contains("ElectricSignBase644"))
                    {
                        dt.Rows[0]["ElectricSignBase644"] = image4;
                    }

                }
                Utility.FillRecordToReport(Report, dt);
            }
            else if (ret.Code == 2)
            {
                CreateDataTableHead(dt, null);
                DataRow dr = dt.NewRow();
                dt.Rows.Add(dr);
                Utility.FillRecordToReport(Report, dt);
            }
            else if (ret.Code == 0)
            {
                MessageBox.Show(ret.M);
            }
        }


        /// <summary>
        /// 加载列头
        /// </summary>
        /// <param name="dt"></param>
        private void CreateDataTableHead(DataTable dt, List<NursingFormRecord> list)
        {
            dt.Columns.Add("PATIENT_NAME", typeof(string));
            dt.Columns.Add("SEX", typeof(string));
            dt.Columns.Add("AGE", typeof(string));
            dt.Columns.Add("NURSE_CELL_CODE", typeof(string));
            dt.Columns.Add("DEPARTMENT_NAME", typeof(string));
            dt.Columns.Add("BED_NO", typeof(string));
            dt.Columns.Add("PATIENT_ID", typeof(string));
            dt.Columns.Add("NURSE_CELL_NAME", typeof(string));

            dt.Columns.Add("hzxx1", typeof(string));
            dt.Columns.Add("hzxx2", typeof(string));
            dt.Columns.Add("hzxx3", typeof(string));
            dt.Columns.Add("hzxx4", typeof(string));

            dt.Columns.Add("RH3", typeof(string));
            dt.Columns.Add("RH4", typeof(string));

            dt.Columns.Add("ShuXueRiQi1", typeof(string));
            dt.Columns.Add("ShuXueRiQi2", typeof(string));
            dt.Columns.Add("ShuXueRiQi3", typeof(string));

            dt.Columns.Add("brjbxx1", typeof(string));
            dt.Columns.Add("brjbxx2", typeof(string));

            dt.Columns.Add("xuexi1", typeof(string));
            dt.Columns.Add("xuexi2", typeof(string));
            dt.Columns.Add("xuexi3", typeof(string));
            dt.Columns.Add("xuexi4", typeof(string));

            dt.Columns.Add("RH1", typeof(string));
            dt.Columns.Add("RH2", typeof(string));

            dt.Columns.Add("xyzl_quanxue", typeof(string));
            dt.Columns.Add("xyzl_xfhxb", typeof(string));
            dt.Columns.Add("xyzl_xdhxb", typeof(string));
            dt.Columns.Add("xyzl_xxb", typeof(string));
            dt.Columns.Add("xyzl_xxbdxj", typeof(string));
            dt.Columns.Add("xyzl_qbdxj", typeof(string));
            dt.Columns.Add("xyzl_lcd", typeof(string));
            dt.Columns.Add("xyzl_qita", typeof(string));

            dt.Columns.Add("xyzl_qita2", typeof(string));
            dt.Columns.Add("xysl_u", typeof(string));
            dt.Columns.Add("xysl_ml", typeof(string));

            dt.Columns.Add("jiaocha1", typeof(string));
            dt.Columns.Add("jiaocha2", typeof(string));
            dt.Columns.Add("xdwg1", typeof(string));
            dt.Columns.Add("xdwg2", typeof(string));

            dt.Columns.Add("xdtxm1", typeof(string));
            dt.Columns.Add("xdtxm2", typeof(string));


            dt.Columns.Add("sxqqm_chz", typeof(string));
            dt.Columns.Add("sxqqm_fhz", typeof(string));
            dt.Columns.Add("cphdqm_chz", typeof(string));
            dt.Columns.Add("cphdqm_fhz", typeof(string));


            dt.Columns.Add("shijian1", typeof(string));
            dt.Columns.Add("shijian2", typeof(string));
            dt.Columns.Add("shijian3", typeof(string));
            dt.Columns.Add("shijian4", typeof(string));
            dt.Columns.Add("shijian5", typeof(string));
            dt.Columns.Add("shijian6", typeof(string));

            dt.Columns.Add("shenzhi1", typeof(string));
            dt.Columns.Add("shenzhi2", typeof(string));
            dt.Columns.Add("shenzhi3", typeof(string));
            dt.Columns.Add("shenzhi4", typeof(string));
            dt.Columns.Add("shenzhi5", typeof(string));
            dt.Columns.Add("shenzhi6", typeof(string));

            dt.Columns.Add("tiwen1", typeof(string));
            dt.Columns.Add("tiwen2", typeof(string));
            dt.Columns.Add("tiwen3", typeof(string));
            dt.Columns.Add("tiwen4", typeof(string));
            dt.Columns.Add("tiwen5", typeof(string));
            dt.Columns.Add("tiwen6", typeof(string));

            dt.Columns.Add("maibo1", typeof(string));
            dt.Columns.Add("maibo2", typeof(string));
            dt.Columns.Add("maibo3", typeof(string));
            dt.Columns.Add("maibo4", typeof(string));
            dt.Columns.Add("maibo5", typeof(string));
            dt.Columns.Add("maibo6", typeof(string));

            dt.Columns.Add("huxi1", typeof(string));
            dt.Columns.Add("huxi2", typeof(string));
            dt.Columns.Add("huxi3", typeof(string));
            dt.Columns.Add("huxi4", typeof(string));
            dt.Columns.Add("huxi5", typeof(string));
            dt.Columns.Add("huxi6", typeof(string));

            dt.Columns.Add("xueya1", typeof(string));
            dt.Columns.Add("xueya2", typeof(string));
            dt.Columns.Add("xueya3", typeof(string));
            dt.Columns.Add("xueya4", typeof(string));
            dt.Columns.Add("xueya5", typeof(string));
            dt.Columns.Add("xueya6", typeof(string));

            dt.Columns.Add("sudu1", typeof(string));
            dt.Columns.Add("sudu2", typeof(string));
            dt.Columns.Add("sudu3", typeof(string));
            dt.Columns.Add("sudu4", typeof(string));
            dt.Columns.Add("sudu5", typeof(string));
            dt.Columns.Add("sudu6", typeof(string));

            dt.Columns.Add("blfy1", typeof(string));
            dt.Columns.Add("blfy2", typeof(string));

            dt.Columns.Add("blfy3", typeof(string));
            dt.Columns.Add("blfy3_1", typeof(string));
            dt.Columns.Add("blfy4", typeof(string));
            dt.Columns.Add("blfy4_1", typeof(string));
            dt.Columns.Add("blfy5", typeof(string));
            dt.Columns.Add("blfy5_1", typeof(string));
            dt.Columns.Add("blfy6", typeof(string));
            dt.Columns.Add("blfy6_1", typeof(string));

            dt.Columns.Add("hsqm1", typeof(string));
            dt.Columns.Add("hsqm2", typeof(string));
            dt.Columns.Add("hsqm3", typeof(string));
            dt.Columns.Add("hsqm4", typeof(string));
            dt.Columns.Add("hsqm5", typeof(string));
            dt.Columns.Add("hsqm6", typeof(string));

            dt.Columns.Add("memoEditbuliangfanyin", typeof(string));
            dt.Columns.Add("xdbu1", typeof(string));
            dt.Columns.Add("xdbu2", typeof(string));
            if (list == null) return;
            NursingFormRecord record1 = list[0];
            Transfuse fd = XmlUtil.XMLToObject<Transfuse>(record1.NursingFormData, Encoding.UTF8);
            //判断是否为空，不为空则添加列
            string SXCHZ = fd.HeDuiNeiRong.SXQHDQM_ChuHeZhe;
            if (!string.IsNullOrEmpty(SXCHZ))
            {
                dt.Columns.Add("ElectricSignBase641", typeof(string));
            }
            string SXFHZ = fd.HeDuiNeiRong.SXQHDQM_FuHeZhe;
            if (!string.IsNullOrEmpty(SXFHZ))
            {
                dt.Columns.Add("ElectricSignBase642", typeof(string));
            }
            string CPCHZ = fd.HeDuiNeiRong.CPHDQM_ChuHeZhe;
            if (!string.IsNullOrEmpty(CPCHZ))
            {
                dt.Columns.Add("ElectricSignBase643", typeof(string));
            }
            string CPFHZ = fd.HeDuiNeiRong.CPHDQM_FuHeZhe;
            if (!string.IsNullOrEmpty(CPFHZ))
            {
                dt.Columns.Add("ElectricSignBase644", typeof(string));
            }
        }
        /// <summary>
        /// 检查数据项目
        /// </summary>
        /// <param name="str"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        private bool ChkStr(string str, int len)
        {
            if (string.IsNullOrEmpty(str)) return false;
            if (str.IndexOf("#") < 0) return false;
            if (str.Split('#').Length < len) return false;
            return true;
        }
        private void RecordsSafePrints_Load(object sender, EventArgs e)
        {
            axGRPrintViewer1.Start();
        }
    }
}
